package com.example.goods_service.controller;

import com.example.goods_service.return_data.Mall_Data;
import com.example.goods_service.service.GoodsServiceImpl_liuyubin;
import com.example.goods_service.vo.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName GoodsController_liuyubin
 * @Author Liu
 * @Date 2021-12-09 16:13
 **/
@RestController
@Api
public class GoodsController_liuyubin {

    @Autowired
    GoodsServiceImpl_liuyubin goodsServiceImpl_liuyubin;

    Mall_Data mall_data = new Mall_Data();

    @ApiOperation("查询评论数（好，中，坏）")
    @RequestMapping("/selectgradedan")
    public int selectgradedan(@RequestParam("id")int id) {
        int n = goodsServiceImpl_liuyubin.gradedan(id);
        System.out.println("正在查询评论");
        return n;
    }

    //全部评论
    @ApiOperation("查询全部评论数(好,中,坏)")
    @RequestMapping("/selectgrade")
    public List<T_user_content_quantity> selectgrade(@RequestParam("id") int grade_count) {
        List<T_user_content_quantity> fupl = goodsServiceImpl_liuyubin.grade(grade_count);
        //   System.out.println("评论数："+grade_count);
        // System.out.println("whole:"+fupl);

        return fupl;
    }
    @ApiOperation("全部评论")
    @RequestMapping("/selectqbpl")
    public List<T_user_content>selectqbpl() {
        List<T_user_content> list = goodsServiceImpl_liuyubin.selectqbpl();
        System.out.println("正在查询评论");
        return list;
    }

    //评论好坏
     @ApiOperation("默认查全部评论和子评论")
    @RequestMapping("/selectcontent")
    public List<T_user_content> selectgradehzh(@RequestParam("id") int id) {
        List<T_user_content> list = goodsServiceImpl_liuyubin.gradehzh(id);
        //  System.out.println("评论内容id："+id);
        //System.out.println("父评论:"+list);
        List<T_user_reply> list1 = goodsServiceImpl_liuyubin.zpl();
        List<T_shop_reply> list2=goodsServiceImpl_liuyubin.selectshoppj();
        // System.out.println(list1.get(0).getComment_id());
        //System.out.println("子评论："+list1);
        //List<T_user_reply>list2=new ArrayList<>();
       //  System.out.println("list"+list);
     //    System.out.println("list2"+list1);
        dtui(list, list1);
        dtui2(list,list2);

         System.out.println("list3"+list);
        //list1.get(i);
        //    list.get(i).setZilist(list1); System.out.println("成功添加父list"+list.get(i).getId());
 /*           for (int j = 0; j <list1.size(); j++) {
               // System.out.println(list1.get(j).getComment_id());
                if ((list.get(i).getId()==list1.get(j).getComment_id())==true){
                    list2.add(list1.get(j));
                   // list.get(i).setZilist(list2);
                    System.out.println("成功添加子list"+list1.get(j).getComment_id());
                }
            }
*/
        // System.out.println(list.get(i).getId()+""+list2);

        // }

        return list;
    }


    @ApiOperation("根据id查询评论等级和子评论 ")
    @RequestMapping("/selectcontenthzh")
    public List<T_user_content> selectgradehzh(@RequestParam("id") int id, @RequestParam("grade") int gradeid) {
        List<T_user_content> list = goodsServiceImpl_liuyubin.gradehzh2(id, gradeid);
        //   System.out.println("评论内容2id："+id);
        // System.out.println("评论等级id："+gradeid);
        // System.out.println("评论list:"+list1);
        // System.out.println("时间:"+list1.get(0).getComment_time());
        List<T_user_reply> list1 = goodsServiceImpl_liuyubin.zpl();
        List<T_shop_reply> list2=goodsServiceImpl_liuyubin.selectshoppj();
        dtui(list, list1);
        dtui2(list,list2);

       // System.out.println(list);
        return list;

    }
    public void dtui(List<T_user_content> biglist, List<T_user_reply> small) {
        for (T_user_content content : biglist) {
            ArrayList<T_user_reply> objects = new ArrayList<>();
            for (T_user_reply reply : small) {
               // System.out.println("子id"+reply.getComment_id());
             //   System.out.println("父id"+content.getId());
                if (reply.getComment_id() == content.getId()) {
                 //   System.out.println("子id与父id相同");
                    objects.add(reply);
                }
            }
            content.setZilist(objects);
        }
    }
    public void dtui2(List<T_user_content> biglist, List<T_shop_reply> small) {
        for (T_user_content content : biglist) {
            ArrayList<T_shop_reply> objects = new ArrayList<>();
            for (T_shop_reply reply : small) {
               // System.out.println("子id"+reply.getComment_id());
             //   System.out.println("父id"+content.getId());
                if (reply.getComment_id() == content.getId()) {
                 //   System.out.println("子id与父id相同");
                    objects.add(reply);
                }
            }
            content.setShop_replies(objects);
        }
    }
    //查询用户
    @ApiOperation("查询用户")
    @RequestMapping("/selectuser")
    public List<T_user> user() {
        List<T_user> list = goodsServiceImpl_liuyubin.user();
        //    System.out.println("用户组："+list);
        return list;
    }
    @ApiOperation("添加子评论")
    @RequestMapping("/insertreply")
    public int insertreply(@RequestParam("commodity_id")int commodity_id,@RequestParam("user_id")int user_id,@RequestParam("comment_id")int comment_id,@RequestParam("reply_content")String replu_content){
        boolean n=  goodsServiceImpl_liuyubin.insertreply(commodity_id, user_id, comment_id, replu_content);
        if (n==true){
            return 1;
        }
        return 2;
    }

      @ApiOperation("根据用户id查询商家回复")
     @RequestMapping("/selectshopreply")
    public List<T_shop_reply> selectshopreply(@RequestParam("userid")int id){
        List<T_shop_reply>list=goodsServiceImpl_liuyubin.selectshopreply(id);
        return list;
     }
     @ApiOperation("/查询商品全部好评数")
     @RequestMapping("/selectqbhp")
     public int selectcontent(@RequestParam("id")int id){
        int n=goodsServiceImpl_liuyubin.selectqbhp(id);
        return n;
     }

     @ApiOperation("/商家浏览")
     @RequestMapping("/selectshopliul")
     public List<T_user_content> selectshop(@RequestParam("id") int id){
        List<T_user_content>list=goodsServiceImpl_liuyubin.selectshopliul(id);
        return list;
     }

     @ApiOperation("/修改评论浏览状态")
     @RequestMapping("/update")
     public int update(@RequestParam("id")int id,@RequestParam("status")int status){
        boolean n=goodsServiceImpl_liuyubin.update(id, status);
        if (n==true){
            return 1;
        }
        else {
            return 2;
        }
     }
    @ApiOperation("/根据id查评论")
     @RequestMapping("/selectidshangp")
     public List<T_user_content>selectsel(@RequestParam("id")int id){
        List<T_user_content>list=goodsServiceImpl_liuyubin.huifsel(id);
        System.out.println(list);
        return list;
     }
     @ApiOperation("/添加商家回复")
     @RequestMapping("/selectinset")
     public int intsert(@RequestParam("spid")int sp,@RequestParam("shop")int shop,@RequestParam("userid") int user,@RequestParam("reply") String reply,@RequestParam("commentid") int commentid){
        boolean n=goodsServiceImpl_liuyubin.insert(sp, shop, user, reply, commentid);
        if (n==true){
            return 1;
        }
        else {
            return 2;
        }
     }

     @RequestMapping("/mhcx")
     public List<T_user_content>mhcx(@RequestParam("value")String value){
        List<T_user_content>list=goodsServiceImpl_liuyubin.mhcx(value);
        return list;
     }

}
